// pages/train/index/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    recommendScene: {},
    hasStudyPower:false,
    hasTrainPower:false,
    hasExamPower:false,
    isRecommended: 'N'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideHomeButton()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(app.globalData.permissionCodes,'=================');
    this.getList()
    this.getRecommendScene()
    const permissionCodes = app.globalData.permissionCodes || []
    console.log('111111====!!!');
    this.setData({
      hasStudyPower:permissionCodes.includes('train_study'),
      hasTrainPower:permissionCodes.includes('rehearse'),
      hasExamPower:permissionCodes.includes('customs_clearance'),
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.setData({
      list:[]
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getList (e) {
    app.api.post({
      url: 'training_vx_list',
    }).then(res => {
      this.setData({ list: res })
    })
  },
  goToDetail (e) {
    const { item } = e.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/train/train-package/trainPackage?trainingLibraryId=${item.id}&trainingLibraryName=${item.trainingLibraryName}`
    })
  },
  imageError (e){
    console.log(e,'e======');
    const _index = e.currentTarget.dataset.index
    const _item = e.currentTarget.dataset.item
    console.log(_index,'data======');
    console.log(_item,'data======');
    this.setData({
      [`list[${_index}].resourceUrl`]:_item.resourceUrl
      // ['lisist[' + _index + ']']:_item
    }, () => {
      console.log(this.data.list)
    })
    // this.setData({
    //   list:[]
    // })
    // this.getList()
    
  },
  getRecommendScene () {
    app.api.get({
      url: 'get_least_recommend_scene'
    }).then(res => {
      let arr = []
      if (res){
        res.map(v => {
          arr.push({
            sceneId: v.id,
            sceneName: v.sceneName,
            description: v.descInfo,
          })
        })
      }
      this.setData({ recommendScene: arr })
    })
  }
})